//
//  WBStatusViewModel.swift
//  SwiftWeibo
//
//  Created by Lausen on 2017/4/25.
//  Copyright © 2017年 id. All rights reserved.
//

import Foundation
/*
 没有任何父类的类想打印描述信息,需要
 1:遵守 CustomStringConvertible 协议
 2:实现 description方法
 */
class WBStatusViewModel: CustomStringConvertible {
    
    /// 单条微博的模型
    var status: WBStatus
    
    /*
     存储型属性:
     用内存换cpu,内存多了可以清理,但是cpu要是消耗太大,就会造成卡顿
     */
    var iconImage: UIImage?
    
    /// 设置认证图片
    var vipIcon: UIImage?
    
    /// 配图视图的尺寸
    var statusPictureSize: CGSize = CGSize()
    
    /// 转发的字符串
    var repostsStr: String?
    /// 评论的字符串
    var commentsStr: String?
    /// 点赞的字符串
    var attitudesStr: String?
    
    /// 缓存cell的行高
    var rowHeight:CGFloat = 0
    
    /// 微博来源  如果在这里计算,需要开辟空间,浪费资源了,不如直接干脆在模型里面用didSet方法计算,不需要开辟空间
//    var sourceStr: String?
    
    /// 不管是原创还是转发,在这个计算性属性里面,把图片的url数组计算出来
    var picURLS: [WBStatusPicture]?{
        
        return status.retweeted_status?.pic_urls ?? status.pic_urls
    }
    
    /// 原创微博的属性文字
    var statusAttrText: NSAttributedString?
    
    /// 转发微博的文字
    var reteewtedAttrText: NSAttributedString?
    
    
    init(model: WBStatus) {
        
        self.status = model
        
        /// 设置iconImage
        /*
         - 尽量减少计算,所有需要的素材提前计算好
         - 空间上不要设置圆角半径,所有图像渲染的属性,都要注意
         - 不要动态创建控件,所有需要创建的空间,都要提前创建好,在现实的时候,根据数据 隐藏 / 显示
         - cell中视图层次越少越好,数量越少越好
         */
        if (model.user?.mbrank)! > 0 && (model.user?.mbrank)! < 7 {
            
            let imageName = "common_icon_membership_level\(status.user?.mbrank ?? 1)"
            
            iconImage = UIImage(named: imageName)
        }

        /// 认证类型. -1:没有认证 0:认证用户 2,3,5企业认证 220:达人
        switch model.user?.verified_type ?? -1{
        case 0:
            vipIcon = UIImage(named: "avatar_vip")
        case 2,3,5:
            vipIcon = UIImage(named: "avatar_enterprise_vip")
        case 220:
            vipIcon = UIImage(named: "avatar_grassroot")
        default:
            break
        }
        
        repostsStr = toString(count: model.reposts_count, defalutStr: "  转发")
        commentsStr = toString(count: model.comments_count, defalutStr: "  评论")
        attitudesStr = toString(count: model.attitudes_count, defalutStr: "  赞")
        
        statusPictureSize = backToPictureViewSize(count: picURLS?.count)
        
        let statusFont = UIFont.systemFont(ofSize: 15)
        let reteewtedFont = UIFont.systemFont(ofSize: 14)
        
        let reteewtedText = "@"
            + (model.retweeted_status?.user?.screen_name ?? "")
            + ":"
            + (model.retweeted_status?.text ?? "")
        
        reteewtedAttrText = LauEmoticonManager.shared.emoticonString(str: reteewtedText ,font: reteewtedFont)
        
        statusAttrText = LauEmoticonManager.shared.emoticonString(str:model.text ?? "" ,font:statusFont)
        
        ///调用缓存行高的方法
        cacheStatusCellRowHeight()
    }
    
    /// 缓存行高的方法
    private func cacheStatusCellRowHeight() {
        //原创微博:顶部分隔视图+间距(12)+头像(34)+间距(12)+正文+配图视图的高+间距(12)+工具栏的高(35)
        //转发微博:顶部分隔视图+间距(12)+头像(34)+间距(12)+正文+转发文本高度+配图视图+间距(12)+工具栏的高(35)
 
        /// 间距
        let margin:CGFloat = 12
        /// 头像图片的高度
        let headHeight:CGFloat = 34
        /// 底部工具栏的高度
        let toolBarHeight:CGFloat = 35
        
        let textSize = CGSize(width: UIScreen.cz_screenWidth() - 2 * margin, height: CGFloat(MAXFLOAT))
        
        var height:CGFloat = 0
        
        height = 3 * margin + headHeight
        
        if let text = statusAttrText{
            
            height += text.boundingRect(with: textSize, options: [.usesLineFragmentOrigin], context: nil).height
        }
        
        if status.retweeted_status != nil {
            
            height += (2 * margin)
            
            if let reteewtedText = reteewtedAttrText {
                
                height += reteewtedText.boundingRect(with: textSize, options: [.usesLineFragmentOrigin], context: nil).height
       
            }
        }
        
        if picURLS?.count != 0 {
            
            height += statusPictureSize.height
        }
        
        height += margin
        
        height += toolBarHeight
        
        rowHeight = height
    }

    func updataSingleImageSize(image:UIImage) {
        
        var size = image.size
        
        /// 图像最大的宽度
        let maxWidth: CGFloat = 300
        /// 图像最小的宽度
        let minWidth:CGFloat = 40
        
        /// 对单张图的过宽或者过窄的处理
        if size.width > maxWidth{
            
            size.width = maxWidth
            size.height = size.width * image.size.height / image.size.width
        }
        
        if size.width < minWidth{
            
            size.width = minWidth
            size.height = size.width * image.size.height / image.size.width
        }
        
        size.height += WBStatusPictureViewOutterMargin
        
        statusPictureSize = size
        
        cacheStatusCellRowHeight()
    }
    
    /// 返回工具栏按钮的标题
    ///
    /// - Parameters:
    ///   - count: 有多少个数
    ///   - defalutStr: 默认的就是按钮的title
    /// - Returns: 返回就是要么是按钮的title要么就是转发数以字符串类型返回
    private func toString(count: Int,defalutStr: String) -> String{
        
        if count == 0 {
            
            return defalutStr
        }
        
        if count < 10000 {
            
            return "\(count)"
        }
        
        return String(format: "%0.2f 万", Double(count) / 10000)
    }
    
    /// 返回配图视图的高度
    ///
    /// - Parameter count: 有多少张配图
    /// - Returns: 返回尺寸
    private func backToPictureViewSize(count: Int?) -> CGSize{
        
        if count == 0 || count == nil {
            return CGSize()
        }
        
        /// 配图的行数
        let row = (count! - 1) / 3 + 1
        
        let height = WBStatusPictureViewOutterMargin
            + CGFloat(row) * itemWidth
            + CGFloat(row - 1) * WBStatusPictureViewInnerMargin
   
        return CGSize(width: WBStatusPictureViewWidth, height:height)
    }
    
    var description: String{
        
        return status.description
    }
}
